//
//  HomePageViewController.m
//  OneDoller
//
//  Created by jyj on 16/8/4.
//  Copyright © 2016年 jyj. All rights reserved.
//

#import "HomePageViewController.h"
#import "define.h"
#import "ShopListCell.h"
#import "ShopListCell2.h"
#import "ShopListCell3.h"

#import "ZoreShopingController.h"
@interface HomePageViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UILabel *navlabel;
@property(nonatomic,strong)UITableView *tableView1;
@property(nonatomic,strong)UITableView *tableView2;
@property(nonatomic,strong)UITableView *tableView3;
//全部订单数组
@property(nonatomic,strong)NSMutableArray *allOrderArr;
//待处理订单数组
@property(nonatomic,strong)NSMutableArray *completeArr;
//待处理订单数组
@property(nonatomic,strong)NSMutableArray *waitDealArr;
//待安装订单数组
@property(nonatomic,strong)NSMutableArray *waitSetupArr;

@end

@implementation HomePageViewController
-(NSMutableArray *)allOrderArr
{
    if (_allOrderArr==nil) {
        
        _allOrderArr=[NSMutableArray array];
        [_allOrderArr addObjectsFromArray:self.completeArr];
        [_allOrderArr addObjectsFromArray:self.waitDealArr];
        [_allOrderArr addObjectsFromArray:self.waitSetupArr];
    }
    return _allOrderArr;
}
-(NSMutableArray *)completeArr
{
    if (_completeArr==nil) {
        _completeArr=
        [@[@1,@2]mutableCopy];
    }
    return _completeArr;
}
-(NSMutableArray *)waitDealArr
{
    if (_waitDealArr==nil) {
        _waitDealArr=
        [@[@1,@2,@3]mutableCopy];
    }
    return _waitDealArr;
}
-(NSMutableArray *)waitSetupArr
{
    
    if (_waitSetupArr==nil) {
        _waitSetupArr=
        [@[@1,@2,@3,@4]mutableCopy];
    }
    return _waitSetupArr;
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 341, kScreenWidth, 282)];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        CGSize size = _scrollView.frame.size;
        size.width *= 3;
        _scrollView.contentSize = size;
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.delegate = self;
        [self.view addSubview:_scrollView];
        
    }
    return _scrollView;
}
-(UILabel *)navlabel
{
    if (!_navlabel) {
        _navlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TOP_VIEW_HEIGHT - NAVIGATION_LINE_HEIGHT, [UIScreen mainScreen].bounds.size.width / 3, NAVIGATION_LINE_HEIGHT)];
        _navlabel.backgroundColor = [UIColor blueColor];
    }
    return _navlabel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor=[UIColor orangeColor];//设置导航栏工具颜色
    //设置scrollView的背景图片
    for(int i=0;i<3;i++){
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(375*i, -64, 375, 128)];
    imageView.image = [UIImage imageNamed:@"homepagebannerplaceholder"];
    [self.ScrollView addSubview:imageView];
    }
    //此处需改用网络版
    
    self.ScrollView.contentSize = CGSizeMake(375*3,0);
    self.ScrollView.pagingEnabled = YES;
    self.ScrollView.showsHorizontalScrollIndicator = NO;



    //设置翻页控制器
    self.PageControl.numberOfPages = 3;
    self.PageControl.currentPage = 0;
    self.PageControl.pageIndicatorTintColor = [UIColor blackColor];
    self.PageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
    
  
    self.ScrollView.delegate = self;
//    NSArray *shoplists = [[NSArray alloc] initWithObjects:@"小火龙",@"杰尼龟",@"妙蛙种子", nil];
//    self.list = shoplists;

    

    
    
    
    
    
    [self createTableView];
    [self registerCell];
    
    
    //定时翻页
    [self addMessageCategoryButton];

    
   [self addTimer];
    


    

}
//注册单元格模板
-(void)registerCell
{
    UINib *nib1=[UINib nibWithNibName:@"ShopListCell" bundle:nil];
    [self.tableView1 registerNib:nib1 forCellReuseIdentifier:@"cell"];
    self.tableView1.rowHeight = kTableViewRowHeight;
    
    UINib *nib2=[UINib nibWithNibName:@"ShopListCell2" bundle:nil];
    [self.tableView2 registerNib:nib2 forCellReuseIdentifier:@"cell2"];
      self.tableView2.rowHeight = kTableViewRowHeight;
    
    UINib *nib3=[UINib nibWithNibName:@"ShopListCell3" bundle:nil];
    [self.tableView3 registerNib:nib3 forCellReuseIdentifier:@"cell3"];
      self.tableView3.rowHeight = kTableViewRowHeight;
}
//创建表格
-(void)createTableView
{
    UITableView *tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 282)style:UITableViewStylePlain];
 
    UITableView *tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, 282)style:UITableViewStylePlain];
    UITableView *tableView3 = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth*2, 0, kScreenWidth, 282)style:UITableViewStylePlain];
    
    self.tableView1 = tableView1;
    self.tableView2 = tableView2;
    self.tableView3 = tableView3;
    self.tableView1.delegate = self;
    self.tableView2.delegate = self;
    self.tableView3.delegate = self;
    self.tableView1.dataSource = self;
    self.tableView2.dataSource = self;
    self.tableView3.dataSource = self;
    [self.scrollView addSubview:tableView1];
    [self.scrollView addSubview:tableView2];
    [self.scrollView addSubview:tableView3];
    
}
//创建消息按钮
- (void)addMessageCategoryButton {
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 281, [UIScreen mainScreen].bounds.size.width, TOP_VIEW_HEIGHT)];
    topView.backgroundColor = [UIColor whiteColor];
    UIButton *approvedMsgButton = [self createMsgButton:@"最新消息" posX:0];
    UIButton *mosMsgButton = [self createMsgButton:@"最高回报" posX:approvedMsgButton.frame.size.width];
    UIButton *systemMsgButton = [self createMsgButton:@"正在进行" posX:approvedMsgButton.frame.size.width * 2];
    
    
    [topView addSubview:approvedMsgButton];
    [topView addSubview:mosMsgButton];
    [topView addSubview:systemMsgButton];
    [topView addSubview:self.navlabel];
    
    [self.view addSubview:topView];
}

//点击按钮后触发事件
- (UIButton *)createMsgButton:(NSString *)title posX:(CGFloat)posX {
    UIButton *msgButton = [[UIButton alloc] initWithFrame:CGRectMake(posX, 0, self.view.frame.size.width / 3, TOP_VIEW_HEIGHT - NAVIGATION_LINE_HEIGHT)];
    msgButton.backgroundColor = [UIColor whiteColor];
    
    NSAttributedString *attributeTitle = [[NSAttributedString alloc] initWithString:title attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:14] }];
    [msgButton setAttributedTitle:attributeTitle forState:UIControlStateNormal];
    msgButton.tag = (NSInteger) (posX / (self.view.frame.size.width / 3));
    self.index = msgButton.tag;
    NSLog(@"%ld",(long)msgButton.tag);
    [msgButton addTarget:self action:@selector(switchMessageDetailView:) forControlEvents:UIControlEventTouchUpInside];
    
    return msgButton;
}
- (void)switchMessageDetailView:(UIButton *)btn {
    [self.scrollView setContentOffset:CGPointMake(btn.tag * self.view.frame.size.width, 0) animated:YES];
    
}

//换图
-(void)nextpage{
    int page = (int)self.PageControl.currentPage;
    if(page == 2)
    {
        page = 0;
    }else
    {
        page++;
    }
    
 //滚动
   CGFloat x = page * 375;
   
    [self.ScrollView setContentOffset:CGPointMake(x, -64) animated:YES];
}
//滚动式调用
#pragma 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   self.PageControl.currentPage = self.ScrollView.contentOffset.x / 375;
    
    CGPoint offset = self.scrollView.contentOffset;
    CGFloat x = offset.x / 3;
    if (x > 0 && x < self.scrollView.frame.size.width) {
        CGRect frame = self.navlabel.frame;
        frame.origin.x = x;
        self.navlabel.frame = frame;

    }

    

}
//拖曳时调用
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
    NSLog(@"启用计时器");
}
-(void)addTimer
{
     self.time = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextpage) userInfo:nil repeats:YES];
}
-(void)removeTimer
{
    [self.time invalidate];
    NSLog(@"移除计时器");
}



#pragma mark1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.index==0) {
        return self.allOrderArr.count;
    }
    else if (self.index==1)
    {
        return self.waitDealArr.count;
    }
    else
    {
        return self.waitSetupArr.count;
    }
  
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
    
}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewCell *cell;
     if (tableView == self.tableView1) {
         ShopListCell *shopCell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
         shopCell.Listlabel.text = self.list[indexPath.row];
         shopCell.image.image = [UIImage imageNamed:@"pokeball"];
         shopCell.image2.image = [UIImage imageNamed:@"pokemon"];
         
        cell = shopCell;
     } else if (tableView == self.tableView2) {
         ShopListCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
         
         cell = cell2;
     } else {
         ShopListCell3 *cell3 = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
         cell = cell3;
     }
     
     return cell;


 }






#pragma mark - buttonTouchUp

//- (IBAction)ZoreShoping:(id)sender {
////  隐藏TabBar的设置
//    self.tabBarController.tabBar.hidden=YES;
//    self.hidesBottomBarWhenPushed=YES;
//    
//    ZoreShopingController *ZoreShoping = [[ZoreShopingController alloc] init];
////    ZoreShoping.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
//    [self.navigationController pushViewController:ZoreShoping animated:YES];
//    self.hidesBottomBarWhenPushed=NO;
//    
//    
//}















































@end
